package com.atguigu.gmall.order.config;


import com.atguigu.gmall.common.constant.RabbitConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@EnableRabbit
@Configuration
public class AppRabbitConfig {


    //容器中的所有 队列，交换机等这些组件，会动态去rabbitmq中自己创建好（如果已经有了就不用创建）；
    //订单事件交换机
    @Bean
    public Exchange orderEventOrder(){
        /**
         * String name, 交换机名字
         * boolean durable,  持久化
         * boolean autoDelete,  是否自动删除
         * Map<String, Object> arguments 交换机的参数设置项
         */
        return new TopicExchange(RabbitConstant.ORDER_EVENT_EXCHANGE,
                true,
                false,
                null);
    }

    //延迟队列; 一个队列可以有任意多个消费者，但是，队列里面的每一个消息，只能被某一个消费者消费
    @Bean
    public Queue orderDelayQueue(){
        /**
         * String name,         队列名
         * boolean durable,     持久化
         * boolean exclusive,  是否排他（允许多个消费者连接）
         * boolean autoDelete, 自动删除
         * @Nullable Map<String, Object> 队列参数设置项
         */
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",RabbitConstant.ORDER_EVENT_EXCHANGE);
        args.put("x-dead-letter-routing-key",RabbitConstant.ORDER_TIMEOU_RK);
        args.put("x-message-ttl",RabbitConstant.ORDER_DELAY_TIME); //ms

        return new Queue(RabbitConstant.ORDER_DELAY_QUEUE,
                true,
                false,
                false,
                args);
    }

    @Bean
    public Binding orderExchangeAndDelayQueueBinding(){

        /**
         * String destination,   目的地
         * Binding.DestinationType destinationType,  目的地类型
         * String exchange,   交换机
         * String routingKey,  路由键
         * @Nullable Map<String, Object> arguments  参数
         *
         * 把 “交换机” 和 “目的地” 使用 “路由键” 进行绑定
         *
         */
        return new Binding(RabbitConstant.ORDER_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EVENT_EXCHANGE,
                RabbitConstant.ORDER_CREATE_RK,
                null);
    }


    //订单死信队列
    @Bean
    public Queue orderDeadQueue(){
        /**
         * String name,
         * boolean durable,
         * boolean exclusive,
         * boolean autoDelete,
         * @Nullable Map<String, Object> argumen
         */
        return new Queue(RabbitConstant.ORDER_DEAD_QUEUE,
                true,
                false,
                false,
                null);
    }

    @Bean
    public Binding orderExchangeAndDeadQueueBinding(){

        /**
         * String destination,
         * Binding.DestinationType destinationType,
         * String exchange,
         * String routingKey,
         * @Nullable Map<String, Object> argu
         */
        return new Binding(
                RabbitConstant.ORDER_DEAD_QUEUE,
                Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EVENT_EXCHANGE,
                RabbitConstant.ORDER_TIMEOU_RK,
                null);
    }


    @Bean
    public Queue orderPayedQueue(){
        /**
         * String name,
         * boolean durable, boolean exclusive, boolean autoDelete,
         *                        @Nullable Map<String, Object> arguments
         */
        return new Queue(RabbitConstant.ORDER_PAYED_QUEUE,
                true,
                false,
                false,
                null);
    }

    @Bean
    public Binding orderPayedBinding(){
        /**
         * String destination,
         * DestinationType destinationType,
         * String exchange,
         * String routingKey,
         *                        @Nullable Map<String, Object> arguments
         */
        return new Binding(RabbitConstant.ORDER_PAYED_QUEUE,
                Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EVENT_EXCHANGE,
                RabbitConstant.ORDER_PAYED_RK,
                null);
    }



    @Bean
    public Queue orderSeckillQueue(){
        /**
         * String name,
         * boolean durable, boolean exclusive, boolean autoDelete,
         *                        @Nullable Map<String, Object> arguments
         */
        return new Queue(RabbitConstant.ORDER_SECKILL_QUEUE,
                true,
                false,
                false,
                null);
    }

    @Bean
    public Binding orderSeckillBinding(){
        /**
         * String destination,
         * DestinationType destinationType,
         * String exchange,
         * String routingKey,
         *                        @Nullable Map<String, Object> arguments
         */
        return new Binding(RabbitConstant.ORDER_SECKILL_QUEUE,
                Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EVENT_EXCHANGE,
                RabbitConstant.ORDER_SECKILL_RK,
                null);
    }



}
